package com.alili.springbooteditor.controller;

import com.alili.springbooteditor.dao.EditorsDao;
import com.alili.springbooteditor.entities.editors;
import com.alili.springbooteditor.repository.EditorRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class articleController {

    @Autowired
    EditorsDao editorsDao;
    @Autowired
    EditorRepository editorRepository;
    //来到登录页面
    @GetMapping("/login")
    public String toLoginPage(){
        return "login";
    }
    //来到普通用户主页面
    @GetMapping("/index")
    public String showArticle(@RequestParam(defaultValue = "0",value = "currentPage") int  pageNum, ModelMap modelMap, Model model){

        Page<editors> p = editorsDao.findAllEditorsAndSortById(pageNum);
        List<editors> allAticle = p.getContent();
        model.addAttribute("pages",p);
        modelMap.addAttribute("arts",allAticle);
        return "index";
    }
//来到管理员主页
@GetMapping("/main")
public String toMainPage(@RequestParam(defaultValue = "0",value = "currentPage") int  pageNum, ModelMap modelMap, Model model){
    Page<editors> p = editorsDao.findAllEditorsAndSortById(pageNum);
    List<editors> allAticle = p.getContent();
    model.addAttribute("pages",p);
    modelMap.addAttribute("arts",allAticle);
    return "manage/main";
}

    //普通用户来到归档页面
    @GetMapping("/archives")
    public String toArchivesPage(@RequestParam(defaultValue = "0",value = "currentPage") int  pageNum, ModelMap modelMap, Model model){
        Page<editors> p = editorsDao.findAllEditorsAndSortByDate(pageNum);//按照日期排序和分页显示
        List<editors> allAticle = p.getContent();
        model.addAttribute("pages",p);
        modelMap.addAttribute("arts",allAticle);
        return "archives";
    }
    //管理员来到归档页面
    @GetMapping("/main/archives")
    public String tomainArchivesPage(@RequestParam(defaultValue = "0",value = "currentPage") int  pageNum, ModelMap modelMap, Model model){
        Page<editors> p = editorsDao.findAllEditorsAndSortByDate(pageNum);//按照日期排序和分页显示
        List<editors> allAticle = p.getContent();
        model.addAttribute("pages",p);
        modelMap.addAttribute("arts",allAticle);
        return "manage/archives";
    }
//普通用户来到搜索页面，显示搜索结果

    @GetMapping("/search")
    public String toSearchPage(@RequestParam(defaultValue = "0",value = "currentPage") int  pageNum,String s, ModelMap modelMap, Model model,Model model2){

        Page<editors> p=editorsDao.findEditorByTitleAndSortByDate(pageNum,s);
        List<editors> allAticle = p.getContent();
        model.addAttribute("pages",p);
        model2.addAttribute("searchS",s);//传递查询的关键字
        modelMap.addAttribute("arts",allAticle);
        return "search";
    }
    //管理员来到搜索页面，显示搜索结果

    @GetMapping("/main/search")
    public String tomainSearchPage(@RequestParam(defaultValue = "0",value = "currentPage") int  pageNum,String s, ModelMap modelMap, Model model,Model model2){
        Page<editors> p=editorsDao.findEditorByTitleAndSortByDate(pageNum,s);
        List<editors> allAticle = p.getContent();
        model.addAttribute("pages",p);
        model2.addAttribute("searchS",s);
        modelMap.addAttribute("arts",allAticle);
        return "manage/search";
    }
}
